package com.coooliang.po;

import org.dom4j.Document;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DocumentModel {
    private String softAuthor;
    private String verison;
    private List<ProjectModel> projects = new ArrayList<>();;

    public DocumentModel toModel(Document document) {
        Element root = document.getRootElement();
        softAuthor = root.elementText("soft-author");
        verison = root.elementText("soft-author");
        List<Element> projectsElement = root.elements("project");
        for (Element projectElement : projectsElement) {
            String name = projectElement.elementText("name");
            String desc = projectElement.elementText("desc");
            String baseUrl = projectElement.elementText("baseUrl");
            ProjectModel projectModel = new ProjectModel();
            projectModel.setName(name);
            projectModel.setDesc(desc);
            projectModel.setBaseUrl(baseUrl);
            List<Element> apisElement = projectElement.element("apis").elements();

            List<ApiModel> apiModelList = new ArrayList<>();
            for (Element apiElement : apisElement) {
                String apiName = apiElement.elementText("name");
                String apiType = apiElement.elementText("type");
                String apiUrl = apiElement.elementText("url");
                ApiModel apiModel = new ApiModel();
                apiModel.setName(apiName);
                apiModel.setType(apiType);
                apiModel.setUrl(apiUrl);
                //req-params
                Element reqParamsElement = apiElement.element("req-params");
                List<Element> reqParamsList = reqParamsElement.elements();
                List<HashMap<String,String>> reqParams = new ArrayList<>();
                for (Element paramElement : reqParamsList) {
                    HashMap<String,String> param = new HashMap<String,String>();
                    param.put("name",paramElement.attribute("name").getValue());
                    param.put("type",paramElement.attribute("type").getValue());
                    param.put("desc",paramElement.attribute("desc").getValue());
                    reqParams.add(param);
                }
                apiModel.setReqParams(reqParams);
                //resp-params
                Element respParamsElement = apiElement.element("resp-params");
                List<Element> reqsParamsList = respParamsElement.elements();
                List<HashMap<String,String>> respParams = new ArrayList<>();
                for (Element paramElement : reqsParamsList) {
                    HashMap<String,String> param = new HashMap<String,String>();
                    param.put("name",paramElement.attribute("name").getValue());
                    param.put("type",paramElement.attribute("type").getValue());
                    param.put("desc",paramElement.attribute("desc").getValue());
                    respParams.add(param);
                }
                apiModel.setRespParams(respParams);

                String respJson = apiElement.elementText("resp-json");
                apiModel.setRespJson(respJson);
                apiModelList.add(apiModel);
            }
            projectModel.setApis(apiModelList);
            projects.add(projectModel);
        }
        return this;
    }

    public String getSoftAuthor() {
        return softAuthor;
    }

    public void setSoftAuthor(String softAuthor) {
        this.softAuthor = softAuthor;
    }

    public String getVerison() {
        return verison;
    }

    public void setVerison(String verison) {
        this.verison = verison;
    }

    public List<ProjectModel> getProjects() {
        return projects;
    }

    public void setProjects(List<ProjectModel> projects) {
        this.projects = projects;
    }
}
